package com.alks.function.service.impl.finance.mtlcosts;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.util.StringUtils;
import com.alks.common.aop.AutoPageAop;
import com.alks.common.data.dto.SysUserTokenDTO;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.beanutils.ArrayUtils;
import com.alks.common.utils.beanutils.BeanUtil;
import com.alks.entity.data.entity.*;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.data.dto.finance.fincons.InboundDetailsDto;
import com.alks.function.data.request.finance.mtlcosts.MtlPreOtherFindRequest;
import com.alks.function.data.request.finance.mtlcosts.MtlPreOtherUpdateRequest;
import com.alks.function.data.request.finance.ryfinance.MtlPreOtherInRequest;
import com.alks.function.mapper.PcSlipDetailSoleMapper;
import com.alks.function.mapper.common.BackMapper;
import com.alks.function.mapper.finance.mtlcosts.PcRecSlipReamrkMapper;
import com.alks.function.mapper.stockdept.pcfactorystockquery.SD_SQ_PcMtlCheckMapper;
import com.alks.function.mapper.stockdept.pcfactorystockquery.SD_SQ_PcSlipDetailPackMapper;
import com.alks.function.mapper.stockdept.pcfactorystockquery.SD_SQ_PcSlipDetailPartBfMapper;
import com.alks.function.service.finance.mtlcosts.IMcRecTbAccountService;
import com.alks.function.service.impl.finance.ryfinance.RecTbAccountServiceImpl;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.alks.common.utils.beanutils.BeanUtil.stringIsEmpty;
import static com.alks.entity.data.enums.ResultCodeEnum.INPUT_ERROR;

/**
 * @author MS
 * @since 2024/6/3上午11:40
 */
@Service
@Slf4j
public class McRecTbAccountServiceImpl extends ServiceImpl<PcRecSlipReamrkMapper, PcRecSlipReamrk> implements IMcRecTbAccountService {
    @Autowired
    private SD_SQ_PcMtlCheckMapper checkMapper;
    @Autowired
    private SD_SQ_PcSlipDetailPartBfMapper partBfMapper;
    @Autowired
    private PcSlipDetailSoleMapper soleMapper;
    @Autowired
    private SD_SQ_PcSlipDetailPackMapper packMapper;
    @Autowired
    private PcRecSlipReamrkMapper reamrkMapper;
    @Autowired
    private BackMapper backMapper;


    @Override
    @AutoPageAop
    public Map<String, Object> mtlPreOtherInQuery(MtlPreOtherInRequest request) {
        String companyId = UserIdThread.get().getCompanyId();
        /*获取数据*/
        List<InboundDetailsDto> dtos = checkMapper.mtlPreOtherInQuery(request, companyId);
        /*获取求和*/
        Map<String, BigDecimal> sum = checkMapper.mtlPreOtherSumQuery(request, companyId);
        /*封装输出*/
        Map<String, Object> map = new HashMap<>(4);
        if (CollectionUtil.isNotEmpty(dtos)) {
            map.put("data", dtos);
        }
        if (CollectionUtil.isNotEmpty(sum)) {
            map.put("qtySum", sum.get("sendQtySum"));
            map.put("amtSum", sum.get("amtSum"));
        }
        if (!request.getPageSize().equals(-1)&&!request.getPageNum().equals(-1)) {
            Page<InboundDetailsDto> page = (Page<InboundDetailsDto>) dtos;
            map.put("total", page.getTotal());
        }
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseInfo mtlPreOtherInUp(List<MtlPreOtherUpdateRequest> request, String remark, String vendorName2) {
        String companyId = UserIdThread.get().getCompanyId();
        if (stringIsEmpty(remark) && stringIsEmpty(vendorName2)) {
            return ResponseInfo.error("备注和厂商不能同时为空", INPUT_ERROR.getCode());
        }
        /*判断厂商是否存在*/
        String vendorNo2 = null;
        if (!stringIsEmpty(vendorName2)) {
            vendorNo2 = backMapper.getVendorNoByName(vendorName2);
            if (stringIsEmpty(vendorNo2)) {
                log.warn("\n异常接口：mtlPreOtherInUp\n异常原因：尝试输入不存在的厂商：{}", vendorName2);
                return ResponseInfo.error("厂商不存在", INPUT_ERROR.getCode());
            }
        }
        for (MtlPreOtherUpdateRequest r : request) {
            /*判断是否存在*/
            LambdaUpdateWrapper<PcRecSlipReamrk> wrapper = new LambdaUpdateWrapper<PcRecSlipReamrk>()
                    .eq(PcRecSlipReamrk::getCompanyId, companyId)
                    .eq(!stringIsEmpty(r.getRecNo()), PcRecSlipReamrk::getRecNo, r.getRecNo())
                    .eq(!stringIsEmpty(r.getLotNo()), PcRecSlipReamrk::getLotNo, r.getLotNo())
                    .eq(!stringIsEmpty(r.getMtlNo()), PcRecSlipReamrk::getMtlNo, r.getMtlNo())
                    .isNull(stringIsEmpty(r.getLotNo()), PcRecSlipReamrk::getLotNo)
                    .isNull(stringIsEmpty(r.getMtlNo()), PcRecSlipReamrk::getMtlNo);
            LambdaUpdateWrapper<PcRecSlipReamrk> wrapper1 = wrapper.last("LIMIT 1");
            PcRecSlipReamrk reamrk = reamrkMapper.selectOne(wrapper1);
            if (BeanUtil.isEmpty(reamrk)) {
                /*不存在则创建*/
                reamrk = BeanUtil.copyBean(r, PcRecSlipReamrk.class);
                reamrk.setCompanyId(companyId);
                reamrk.setRemark(remark);
                reamrk.setVendorNo(vendorNo2);
                reamrk.setVendorName(vendorName2);
                reamrk.setCfmDate(LocalDateTime.now());
                reamrkMapper.insert(reamrk);
            } else {
                /*如果存在则更新数据*/
                System.out.println(remark);
                System.out.println(vendorName2);
                wrapper.set(StringUtil.isNotBlank(remark), PcRecSlipReamrk::getRemark, remark)
                        .set(StringUtil.isNotBlank(vendorName2), PcRecSlipReamrk::getVendorName, vendorName2)
                        .set(StringUtil.isNotBlank(vendorName2), PcRecSlipReamrk::getVendorNo, vendorNo2);
                reamrkMapper.update(null,wrapper);
            }
        }
        return ResponseInfo.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseInfo mtlPreOtherInFinance(List<MtlPreOtherFindRequest> request, String financeYymm, String accountName, Integer flag) {
        SysUserTokenDTO tokenDTO = UserIdThread.get();
        String companyId = tokenDTO.getCompanyId();
        String userName = tokenDTO.getUserName();
        String loginName = tokenDTO.getLoginName();
        /*入参检测*/
        if (flag.equals(1)) {
            if (stringIsEmpty(financeYymm)) {
                return ResponseInfo.error("对账月份不能为空");
            }
            if (stringIsEmpty(accountName)) {
                return ResponseInfo.error("对账方式不能为空");
            }
            RecTbAccountServiceImpl.financeDateCheck(financeYymm);
        }
        for (MtlPreOtherFindRequest r : request) {
            switch (r.getSeqName()) {
                case "面材":
                    LambdaUpdateWrapper<PcMtlCheck> checkWrapper = new LambdaUpdateWrapper<PcMtlCheck>()
                            .eq(PcMtlCheck::getPrimaryId,r.getId());
//                            .eq(PcMtlCheck::getCompanyId, companyId)
//                            .eq(PcMtlCheck::getRecNo, r.getRecNo())
//                            .eq(StringUtils.isNotBlank(r.getReqNo()),PcMtlCheck::getReqNo,r.getReqNo())
//                            .isNull(StringUtils.isBlank(r.getReqNo()),PcMtlCheck::getReqNo)
//                            .eq(!stringIsEmpty(r.getLotNo()), PcMtlCheck::getPurLot, r.getLotNo())
//                            .isNull(stringIsEmpty(r.getLotNo()), PcMtlCheck::getPurLot)
//                            .eq(!stringIsEmpty(r.getMtlNo()), PcMtlCheck::getMtlNo, r.getMtlNo())
//                            .isNull(stringIsEmpty(r.getMtlNo()), PcMtlCheck::getMtlNo)
//                            .eq(r.getRecDate() != null, PcMtlCheck::getRecDate, r.getRecDate())
//                            .isNull(r.getRecDate() == null, PcMtlCheck::getRecDate)
//                            .eq(!stringIsEmpty(r.getVendorName()), PcMtlCheck::getVendorName, r.getVendorName())
//                            .isNull(stringIsEmpty(r.getVendorName()), PcMtlCheck::getVendorName);
                    List<PcMtlCheck> mtlChecks = checkMapper.selectList(checkWrapper);
                    if (ArrayUtils.isEmpyt(mtlChecks)) {
                        log.error("\n错误接口：mtlPreOtherInFinance\n错误原因：没有找到对应数据\n所在表：【PC_MTL_CHECK】\n入参：{}", r);
                        throw new ServiceErrorException("没有找到数据");
                    } else if (mtlChecks.size() > 1) {
                        log.warn("\n异常接口：mtlPreOtherInFinance\n异常原因：定位到的数据不唯一\n所在表:【PC_MTL_ACC】\n入参：{}", r);
                    }
                    if (flag.equals(1)) {
                        List<PcMtlCheck> cfms = mtlChecks.stream()
                                .filter(a -> "Y".equals(a.getFinanceCfmFlag()))
                                .collect(Collectors.toList());
                        if (ArrayUtils.isNoEmpyt(cfms)) {
                            log.error("\n错误接口：mtlPreOtherInFinance\n错误原因：尝试对账已对账数据\n入参：{}", r);
                            throw new ServiceErrorException("订单" + r.getRecNo() + "已对账");
                        }
                        checkWrapper.set(PcMtlCheck::getFinanceCfmFlag, "Y")
                                .set(PcMtlCheck::getFinanceCfmUser, loginName)
                                .set(PcMtlCheck::getFinanceCfmDate, LocalDate.now())
                                .set(PcMtlCheck::getFinanceRecYymm, financeYymm)
                                .set(PcMtlCheck::getAccountName, accountName);
                    } else if (flag.equals(0)) {
                        List<PcMtlCheck> unCfmList = mtlChecks.stream()
                                .filter(a -> StringUtils.isNotBlank(a.getBalanceNo()))
                                .collect(Collectors.toList());
                        if (!ArrayUtils.isEmpyt(unCfmList)) {
                            log.error("\n错误接口：mtlPreOtherInFinance\n错误原因：尝试取消已应付数据\n入参：{}", r);
                            throw new ServiceErrorException("订单" + r.getRecNo() + "已应付");
                        }
                        List<String> cfms = mtlChecks.stream()
                                .map(PcMtlCheck::getFinanceCfmFlag)
                                .filter(Objects::isNull)
                                .collect(Collectors.toList());
                        if (ArrayUtils.isNoEmpyt(cfms)) {
                            log.error("\n错误接口：mtlPreOtherInFinance\n错误原因：尝试取消未对账数据\n入参：{}", r);
                            throw new ServiceErrorException("订单" + r.getRecNo() + "未对账");
                        }
                        checkWrapper.set(PcMtlCheck::getFinanceCfmFlag, null)
                                .set(PcMtlCheck::getFinanceCfmUser, null)
                                .set(PcMtlCheck::getFinanceCfmDate, null)
                                .set(PcMtlCheck::getFinanceRecYymm, null)
                                .set(PcMtlCheck::getAccountName, null);
                    }
                    checkMapper.update(null, checkWrapper);
                    break;
                case "包材":
                    LambdaUpdateWrapper<PcSlipDetailPack> packWrapper = new LambdaUpdateWrapper<PcSlipDetailPack>()
                            .eq(PcSlipDetailPack::getId, r.getId());
//                            .eq(PcSlipDetailPack::getCompanyId, companyId)
//                            .eq(PcSlipDetailPack::getSlipNo, r.getRecNo())
//                            .eq(!stringIsEmpty(r.getLotNo()), PcSlipDetailPack::getLotNo, r.getLotNo())
//                            .eq(!stringIsEmpty(r.getMtlNo()), PcSlipDetailPack::getMtlNo, r.getMtlNo())
//                            .eq(r.getRecDate() != null, PcSlipDetailPack::getSlipDate, r.getRecDate())
//                            .eq(!stringIsEmpty(r.getVendorName()), PcSlipDetailPack::getVendorName, r.getVendorName())
//                            .isNull(stringIsEmpty(r.getLotNo()), PcSlipDetailPack::getLotNo)
//                            .isNull(stringIsEmpty(r.getMtlNo()), PcSlipDetailPack::getMtlNo)
//                            .isNull(r.getRecDate() == null, PcSlipDetailPack::getSlipDate)
//                            .isNull(stringIsEmpty(r.getVendorName()), PcSlipDetailPack::getVendorName);
                    List<PcSlipDetailPack> packs = packMapper.selectList(packWrapper);
                    if (ArrayUtils.isEmpyt(packs)) {
                        log.error("\n错误接口：mtlPreOtherInFinance\n错误原因：没有找到对应数据\n所在表：【PC_SLIP_DETAIL_PACK】\n入参：{}", r);
                        throw new ServiceErrorException("没有找到数据");
                    } else if (packs.size() > 1) {
                        log.warn("\n异常接口：mtlPreOtherInFinance\n异常原因：定位到的数据不唯一\n所在表:【PC_SLIP_DETAIL_PACK】\n入参：{}", r);
                    }
                    if (flag.equals(1)) {
                        List<PcSlipDetailPack> cfms = packs.stream()
                                .filter(a -> "Y".equals(a.getFinanceCfmFlag()))
                                .collect(Collectors.toList());
                        if (ArrayUtils.isNoEmpyt(cfms)) {
                            log.error("\n错误接口：mtlPreOtherInFinance\n错误原因：尝试对账已对账数据\n入参：{}", r);
                            throw new ServiceErrorException("订单" + r.getRecNo() + "已对账");
                        }
                        packWrapper.set(PcSlipDetailPack::getFinanceCfmFlag, "Y")
                                .set(PcSlipDetailPack::getFinanceCfmUser, loginName)
                                .set(PcSlipDetailPack::getFinanceCfmDate, LocalDate.now())
                                .set(PcSlipDetailPack::getFinanceYymm, financeYymm)
                                .set(PcSlipDetailPack::getAccountName, accountName);
                    } else if (flag.equals(0)) {
                        List<String> balanceNos = packs.stream()
                                .map(PcSlipDetailPack::getBalanceNo)
                                .filter(Objects::nonNull)
                                .collect(Collectors.toList());
                        if (!ArrayUtils.isEmpyt(balanceNos)) {
                            log.error("\n错误接口：mtlPreOtherInFinance\n错误原因：尝试取消已应付数据\n入参：{}", r);
                            throw new ServiceErrorException("订单" + r.getRecNo() + "已应付");
                        }
                        List<PcSlipDetailPack> unCfmList = packs.stream()
                                .filter(a -> StringUtils.isBlank(a.getFinanceCfmFlag()))
                                .collect(Collectors.toList());
                        if (!ArrayUtils.isEmpyt(unCfmList)) {
                            log.error("\n错误接口：mtlPreOtherInFinance\n错误原因：尝试取消未对账数据\n入参：{}", r);
                            throw new ServiceErrorException("订单" + r.getRecNo() + "未对账");
                        }
                        packWrapper.set(PcSlipDetailPack::getFinanceCfmFlag, null)
                                .set(PcSlipDetailPack::getFinanceCfmUser, null)
                                .set(PcSlipDetailPack::getFinanceCfmDate, null)
                                .set(PcSlipDetailPack::getFinanceYymm, null)
                                .set(PcSlipDetailPack::getAccountName, null);
                    }
                    packMapper.update(null, packWrapper);
                    break;
                case "大底":
                case "面衬":
                    String[] split = r.getMtlName().split("-");
                    LambdaUpdateWrapper<PcSlipDetailSole> soleWrapper = new LambdaUpdateWrapper<PcSlipDetailSole>()
                            .eq(PcSlipDetailSole::getCompanyId, companyId)
                            .eq(PcSlipDetailSole::getSlipNo, r.getRecNo())
                            .eq(PcSlipDetailSole::getPrice, r.getPrice())
                            .eq(!stringIsEmpty(r.getLotNo()), PcSlipDetailSole::getLotNo, r.getLotNo())
                            .eq(r.getRecDate() != null, PcSlipDetailSole::getSlipDate, r.getRecDate())
                            .eq(!stringIsEmpty(r.getSendNo()), PcSlipDetailSole::getSendNo, r.getSendNo())
                            .eq(!stringIsEmpty(r.getVendorName()), PcSlipDetailSole::getVendorName, r.getVendorName())
                            .apply("CONVERT(SIZE_NO, DECIMAL) >= CONVERT("+ split[1]+", DECIMAL)")
                            .apply("CONVERT(SIZE_NO, DECIMAL) <= CONVERT("+ split[0]+", DECIMAL)")
                            .isNull(stringIsEmpty(r.getLotNo()), PcSlipDetailSole::getLotNo)
                            .isNull(r.getRecDate() == null, PcSlipDetailSole::getSlipDate)
                            .isNull(stringIsEmpty(r.getVendorName()), PcSlipDetailSole::getVendorName);
                    List<PcSlipDetailSole> soles = soleMapper.selectList(soleWrapper);
                    if (ArrayUtils.isEmpyt(soles)) {
                        log.error("\n错误接口：mtlPreOtherInFinance\n错误原因：没有找到对应数据\n所在表：【PC_SLIP_DETAIL_SOLE】\n入参：{}", r);
                        throw new ServiceErrorException("没有找到数据");
                    }
                    if (flag.equals(1)) {
                        List<PcSlipDetailSole> cfms = soles.stream()
                                .filter(a -> "Y".equals(a.getFinanceCfmFlag()))
                                .collect(Collectors.toList());
                        if (!ArrayUtils.isEmpyt(cfms)) {
                            log.error("\n错误接口：mtlPreOtherInFinance\n错误原因：尝试对账已对账数据\n入参：{}", r);
                            throw new ServiceErrorException("订单" + r.getRecNo() + "已对账");
                        }
                        soleWrapper.set(PcSlipDetailSole::getFinanceCfmFlag, "Y")
                                .set(PcSlipDetailSole::getFinanceCfmUser, loginName)
                                .set(PcSlipDetailSole::getFinanceCfmDate, LocalDate.now())
                                .set(PcSlipDetailSole::getFinanceYymm, financeYymm)
                                .set(PcSlipDetailSole::getAccountName, accountName);
                    } else if (flag.equals(0)) {
                        List<String> balanceNos = soles.stream()
                                .map(PcSlipDetailSole::getBalanceNo)
                                .filter(Objects::nonNull)
                                .collect(Collectors.toList());
                        if (!ArrayUtils.isEmpyt(balanceNos)) {
                            log.error("\n错误接口：mtlPreOtherInFinance\n错误原因：尝试取消已应付数据\n入参：{}", r);
                            throw new ServiceErrorException("订单" + r.getRecNo() + "已应付");
                        }
                        List<String> cfms = soles.stream()
                                .map(PcSlipDetailSole::getFinanceCfmFlag)
                                .filter(Objects::isNull)
                                .collect(Collectors.toList());
                        if (!ArrayUtils.isEmpyt(cfms)) {
                            log.error("\n错误接口：mtlPreOtherInFinance\n错误原因：尝试取消未对账数据\n入参：{}", r);
                            throw new ServiceErrorException("订单" + r.getRecNo() + "未对账");
                        }
                        soleWrapper.set(PcSlipDetailSole::getFinanceCfmFlag, null)
                                .set(PcSlipDetailSole::getFinanceCfmUser, null)
                                .set(PcSlipDetailSole::getFinanceCfmDate, null)
                                .set(PcSlipDetailSole::getFinanceYymm, null)
                                .set(PcSlipDetailSole::getAccountName, null);
                    }
                    soleMapper.update(null, soleWrapper);
                    break;
                case "易耗品":
                    LambdaUpdateWrapper<PcSlipDetailPartBf> partBfWrappper = new LambdaUpdateWrapper<PcSlipDetailPartBf>()
                            .eq(PcSlipDetailPartBf::getId,r.getId());
//                            .eq(PcSlipDetailPartBf::getCompanyId, companyId)
//                            .eq(PcSlipDetailPartBf::getSlipNo, r.getRecNo())
//                            .eq(!stringIsEmpty(r.getLotNo()), PcSlipDetailPartBf::getLotNo, r.getLotNo())
//                            .eq(!stringIsEmpty(r.getMtlNo()), PcSlipDetailPartBf::getMtlNo, r.getMtlNo())
//                            .eq(r.getRecDate() != null, PcSlipDetailPartBf::getSlipDate, r.getRecDate())
//                            .eq(!stringIsEmpty(r.getVendorName()), PcSlipDetailPartBf::getVendorName, r.getVendorName())
//                            .isNull(stringIsEmpty(r.getLotNo()), PcSlipDetailPartBf::getLotNo)
//                            .isNull(stringIsEmpty(r.getMtlNo()), PcSlipDetailPartBf::getMtlNo)
//                            .isNull(r.getRecDate() == null, PcSlipDetailPartBf::getSlipDate)
//                            .isNull(stringIsEmpty(r.getVendorName()), PcSlipDetailPartBf::getVendorName);
                    List<PcSlipDetailPartBf> partBfs = partBfMapper.selectList(partBfWrappper);
                    if (ArrayUtils.isEmpyt(partBfs)) {
                        log.error("\n错误接口：mtlPreOtherInFinance\n错误原因：没有找到对应数据\n所在表：【PC_SLIP_DETAIL_PART_BF】\n入参：{}", r);
                        throw new ServiceErrorException("没有找到数据");
                    } else if (partBfs.size() > 1) {
                        log.warn("\n异常接口：mtlPreOtherInFinance\n异常原因：定位到的数据不唯一\n所在表:【PC_SLIP_DETAIL_PART_BF】\n入参：{}", r);
                    }
                    if (flag.equals(1)) {
                        List<PcSlipDetailPartBf> cfms = partBfs.stream()
                                .filter(a -> "Y".equals(a.getFinanceCfmFlag()))
                                .collect(Collectors.toList());
                        if (!ArrayUtils.isEmpyt(cfms)) {
                            log.error("\n错误接口：mtlPreOtherInFinance\n错误原因：尝试对账已对账数据\n入参：{}", r);
                            throw new ServiceErrorException("订单" + r.getRecNo() + "已对账");
                        }
                        partBfWrappper.set(PcSlipDetailPartBf::getFinanceCfmFlag, "Y")
                                .set(PcSlipDetailPartBf::getFinanceCfmUser, loginName)
                                .set(PcSlipDetailPartBf::getFinanceCfmDate, LocalDate.now())
                                .set(PcSlipDetailPartBf::getFinanceYymm, financeYymm)
                                .set(PcSlipDetailPartBf::getAccountName, accountName);
                    } else if (flag.equals(0)) {
                        List<PcSlipDetailPartBf> unCfmList = partBfs.stream()
                                .filter(a -> StringUtils.isBlank(a.getFinanceCfmFlag()))
                                .collect(Collectors.toList());
                        if (!ArrayUtils.isEmpyt(unCfmList)) {
                            log.error("\n错误接口：mtlPreOtherInFinance\n错误原因：尝试取消已应付数据\n入参：{}", r);
                            throw new ServiceErrorException("订单" + r.getRecNo() + "已应付");
                        }
                        List<String> cfms = partBfs.stream()
                                .map(PcSlipDetailPartBf::getFinanceCfmFlag)
                                .filter(Objects::isNull)
                                .collect(Collectors.toList());
                        if (!ArrayUtils.isEmpyt(cfms)) {
                            log.error("\n错误接口：mtlPreOtherInFinance\n错误原因：尝试取消未对账数据\n入参：{}", r);
                            throw new ServiceErrorException("订单" + r.getRecNo() + "未对账");
                        }
                        partBfWrappper.set(PcSlipDetailPartBf::getFinanceCfmFlag, null)
                                .set(PcSlipDetailPartBf::getFinanceCfmUser, null)
                                .set(PcSlipDetailPartBf::getFinanceCfmDate, null)
                                .set(PcSlipDetailPartBf::getFinanceYymm, null)
                                .set(PcSlipDetailPartBf::getAccountName, null);
                    }
                    partBfMapper.update(null, partBfWrappper);
                default:
                    break;
            }
        }
        return ResponseInfo.ok();
    }

    @Override
    public ResponseInfo mtlPreOtherInBack() {
        String companyId = UserIdThread.get().getCompanyId();
        /*备注回显*/
        List<String> remark = lambdaQuery()
                .select(PcRecSlipReamrk::getRemark)
                .eq(PcRecSlipReamrk::getCompanyId, companyId)
                .groupBy(PcRecSlipReamrk::getRemark).list()
                .stream().filter(Objects::nonNull)
                .map(PcRecSlipReamrk::getRemark).collect(Collectors.toList());
        return ResponseInfo.ok(remark);
    }

    @Override
    public ResponseInfo mtlPreUpRemark(String seqName, Integer id, String remark) {
//        String companyId = UserIdThread.get().getCompanyId();
//        /*分别处理*/
//        switch (seqName){
//            case "面材":
//                LambdaUpdateWrapper<PcMtlCheck> mtlCheckWrapper = new LambdaUpdateWrapper<PcMtlCheck>()
//                        .eq(PcMtlCheck::getCompanyId, companyId)
//                        .eq(PcMtlCheck::getPrimaryId, id)
//                        .set(PcMtlCheck::getFinanceRecMemo, remark);
//                checkMapper.update(null, mtlCheckWrapper);
//                break;
//            case "包材":
//                LambdaUpdateWrapper<PcSlipDetailPack> packWrapper = new LambdaUpdateWrapper<PcSlipDetailPack>()
//                        .eq(PcSlipDetailPack::getCompanyId, companyId)
//                        .eq(PcSlipDetailPack::getId, id)
//                        .set(PcSlipDetailPack::getFinanceRecMemo, remark);
//                packMapper.update(null, packWrapper);
//                break;
//            case "大底":
//            case "面衬":
//                //大底面材是聚合数据 需要一次性更新全部关联数据
//                /*找出指定数据*/
//                LambdaQueryWrapper<PcSlipDetailSole> soleWrapper = new LambdaQueryWrapper<PcSlipDetailSole>()
//                        .eq(PcSlipDetailSole::getId,id)
//                        .eq(PcSlipDetailSole::getCompanyId,companyId);
//                PcSlipDetailSole sole = soleMapper.selectOne(soleWrapper);
//                /*拓展更新全部相关数据*/
//                LambdaUpdateWrapper<PcSlipDetailSole> soleUpWrapper = new LambdaUpdateWrapper<PcSlipDetailSole>()
//                        .set(PcSlipDetailSole::getFinanceMemo,remark)
//                        .eq(PcSlipDetailSole::getCompanyId,companyId)
//                        .eq(PcSlipDetailSole::getSlipNo,sole.getSlipNo())
//                        .eq(PcSlipDetailSole::getLotNo,sole.getLotNo())
//                        .eq(PcSlipDetailSole::getPrice,sole.getPrice())
//                        .eq(PcSlipDetailSole::getAccountName,sole.getAccountName())
//                        .eq(PcSlipDetailSole::getFinanceYymm,sole.getFinanceYymm())
//                        .eq(PcSlipDetailSole::getFinanceCfmFlag,sole.getFinanceCfmFlag())
//                        .eq(PcSlipDetailSole::getSendNo,sole.getSendNo());
//                soleMapper.update(null,soleUpWrapper);
//                break;
//            case "易耗品":
//                LambdaUpdateWrapper<PcSlipDetailPartBf> partBfWrapper = new LambdaUpdateWrapper<PcSlipDetailPartBf>()
//                        .eq(PcSlipDetailPartBf::getCompanyId, companyId)
//                        .eq(PcSlipDetailPartBf::getId, id)
//                        .set(PcSlipDetailPartBf::getFinanceMemo, remark);
//                partBfMapper.update(null,partBfWrapper);
//                break;
//            default:
//                return ResponseInfo.error("类型错误");
//        }
        return ResponseInfo.warn("请返回检核页面修改备注");
    }

    @Override
    public ResponseInfo mtlPreOtherInUp2(List<MtlPreOtherUpdateRequest> request) {
            String companyId = UserIdThread.get().getCompanyId();
            for (MtlPreOtherUpdateRequest r : request) {
                /*判断是否存在*/
                LambdaUpdateWrapper<PcRecSlipReamrk> wrapper = new LambdaUpdateWrapper<PcRecSlipReamrk>()
                        .eq(PcRecSlipReamrk::getCompanyId, companyId)
                        .eq(!stringIsEmpty(r.getRecNo()), PcRecSlipReamrk::getRecNo, r.getRecNo())
                        .eq(!stringIsEmpty(r.getLotNo()), PcRecSlipReamrk::getLotNo, r.getLotNo())
                        .eq(!stringIsEmpty(r.getMtlNo()), PcRecSlipReamrk::getMtlNo, r.getMtlNo())
                        .isNull(stringIsEmpty(r.getLotNo()), PcRecSlipReamrk::getLotNo)
                        .isNull(stringIsEmpty(r.getMtlNo()), PcRecSlipReamrk::getMtlNo);
                LambdaUpdateWrapper<PcRecSlipReamrk> wrapper1 = wrapper.last("LIMIT 1");
                PcRecSlipReamrk reamrk = reamrkMapper.selectOne(wrapper1);
                if (BeanUtil.isNotEmpty(reamrk)) {
                    /*如果存在则更新数据*/
                    wrapper.set(PcRecSlipReamrk::getVendorName, null)
                            .set(PcRecSlipReamrk::getVendorNo, null);
                    reamrkMapper.update(null,wrapper);
                }
            }
            return ResponseInfo.ok();}

}
